<!-- 疫病库查询 -->
<template>
  <div>
    <ContainerSlot>
      <template slot="searchModule">
        <SearchForm :label-width="labelWidth" :form-config="formConfig" :form-data="formData" :slot-list="slotList">
          <template slot="searchform">
            <!-- 查询表单 -->
            <div class="select-box-container">
              <!-- 关联品种 -->
              <div class="select-box-item">
                <span>关联品种：</span>
                <el-select v-model="formData.fishVarietyId" filterable clearable @change="qureyFeaturesTypeManage"
                  @visible-change="handleSelectVisibleChange">
                  <el-option v-for="item of FishVarietyList" :key="item.fishVarietyId" :label="item.varietyName"
                    :value="item.fishVarietyId" />
                </el-select>
              </div>
              <!-- 诊断项名称和选择诊断项症状编号 -->
              <div class="select-box-item name-number" v-for="(item, index) of BOList" :key="index">
                <!-- 诊断项名称 -->
                <div class="select-box-item-one">
                  <span>诊断项名称：</span>
                  <el-select v-model="item.diagnosisItemId" filterable clearable
                    @change="qureyDiseaseFeatures($event, index)">
                    <el-option v-for="item of FeaturesTypeManageList" :key="item.diagnosisItemId + 'FeaturesType'"
                      :label="item.mainFeaturesFlag ? item.diagnosisItemName + '（主要特征）' : item.diagnosisItemName + '（辅助特征）'"
                      :value="item.diagnosisItemId" :disabled="disabledList.indexOf(item.diagnosisItemId) !== -1" />
                  </el-select>
                </div>
                <!-- 选择诊断项症状 -->
                <div class="select-box-item-three" style="width:400px;">
                  <span>选择诊断项症状： </span>
                  <el-select v-model="item.diagnosisItemSymbolNoList" collapse-tags clearable multiple
                    :ref="'ref' + index" @visible-change="updateImg" @change="updateSelected">
                    <el-option v-for="item of BOList[index].diseaseFeaturesList" :key="item.diagnosisItemSymbolId"
                      :label="item.diagnosisItemSymbolNo + '：' + item.diagnosisItemSymbolDesc"
                      :value="item.diagnosisItemSymbolNo" @mouseover.native="showAddress(item.diagnosisItemSymbolDesc)"
                      @mouseout.native="closeShowAddress">
                      <!-- 列表数据 -->
                      {{ item.diagnosisItemSymbolNo + '：' + item.diagnosisItemSymbolDesc }}
                      <!-- 提示框 -->
                      <div v-show="isshow" class="dsdTooltip">{{ diagnosisItemSymbolDesc }}</div>
                    </el-option>
                  </el-select>
                </div>
                <!-- 按钮样式 -->
                <div class="select-box-item-son">
                  <!-- 减少按钮 -->
                  <div v-if="BOList.length !== 1" class="line-click" @click="deleteRow(index)">
                    <i class="el-icon-remove-outline"></i>
                  </div>
                  <!-- 添加按钮 -->
                  <div v-if="index === BOList.length - 1" class="line-click" @click="addRow">
                    <i class="el-icon-circle-plus-outline"></i>
                  </div>
                </div>
              </div>
            </div>

            <!-- 推断原则 -->
            <div class="epidemic-database">
              <span class="epidemic-database-star">✮</span>推断原则：A、B、C、F是主要判断特征，只要符合其中1-2条或以上即可推定属于该病；D、E是辅助判断特征。
            </div>
          </template>
          <template slot="searchBtn">
            <el-button type="primary" @click.stop="judgeDisease">查询</el-button>
          </template>
        </SearchForm>
      </template>

      <!-- 特征图片 -->
      <template slot="contentModule">
        <div class="sectionOutermost">
          <!--  -->
          <div class="section-center">
            <div class="selected-box">
              <span>已选特征:</span>
              <span v-for="(item, index) of formData.diagnosisItemSymbolNoList" :key="index" class="selected-btn">{{ item
              }}</span>
            </div>
            <div class="picture-box">
              <div class="diagnosis" v-for="(item, index) of selectNoList" :key="index">
                <div class="diagnosis-header">
                  <span class="diagnosis-header-title">{{ item.diagnosisItemSymbolNo }}</span>
                  <el-tooltip class="item" effect="dark" :content="item.diagnosisItemSymbolDesc" placement="top-start">
                    <span class="diagnosis-header-desc">- {{ item.diagnosisItemSymbolDesc }}</span>
                  </el-tooltip>
                </div>
                <div class="diagnosis-body">
                  <el-image v-if="item.src && item.src.length" :src="item.src[0]" :preview-src-list="item.src"
                    style="width: 100%; height: 100%">
                  </el-image>
                  <div v-if="item.src && item.src.length" class="numFont">共{{ item.src.length }}张</div>
                  <span v-if="!item.src" class="diagnosis-body-empty">暂无图片展示</span>
                </div>
              </div>
            </div>
          </div>

          <!-- 预防/治疗方法 -->
          <div class="section-bottom">
            <div class="result-item" v-for="(item, index) of resultList" :key="index + 'result'">
              <div class="result-item-row">
                <span class="result-item-row-title">推断结果：</span>
                <span class="result-item-row-content" style="color: #0480FB;">{{ item.diseaseName }}</span>
              </div>
              <div class="result-item-row">
                <span class="result-item-row-title">预防方法：</span>
                <span class="result-item-row-content">{{ item.preventionMethod }}</span>
              </div>
              <div class="result-item-row">
                <span class="result-item-row-title">治疗方法：</span>
                <span class="result-item-row-content">{{ item.therapeuticMethod }}</span>
              </div>
              <div class="result-item-row">
                <span class="result-item-row-title">特殊症状描述：</span>
                <span class="result-item-row-content">{{ item.mainSymptoms }}</span>
              </div>
              <div class="result-item-row">
                <span class="result-item-row-title">疾病可能性：</span>
                <span class="result-item-row-content">{{ item.ratio ? item.ratio + '%' : '--%' }}</span>
              </div>
            </div>
          </div>
        </div>
      </template>
    </ContainerSlot>
  </div>
</template>

<script>
// import ContainerSlot from '@/components/Container'
import ContainerSlot from "./components/Container.vue";
import SearchForm from "./components/form.vue";
import Table from "@/components/Table";
import { blobToBase64 } from "./components/convert";

import DiseaseManageApi from "@/api/disease-bank-manage/disease-manage/index";                  // 疾病库（判断疾病）
import FishVarietyApi from "@/api/fishVariety/index";                                           // 鱼类品种管理
import FeaturesTypeManageApi from "@/api/disease-bank-manage/features-type-manage/index";       // 诊断项
import DiseaseFeaturesManageApi from "@/api/disease-bank-manage/disease-features-manage/index"; // 诊断项症状
import newDocumentApi from "@/api/newdocument";                                                 // 图片上传/下载

export default {
  name: "",
  components: { ContainerSlot, SearchForm, Table },
  data () {
    return {
      isSelectVisible: false, // 是否是下拉框出现触发
      isshow: false,               // 诊断项症状提示框 显示/隐藏
      diagnosisItemSymbolDesc: '', // 诊断项症状提示框功能
      labelWidth: "100px", // 表单项宽度
      // 查询栏 - 表单配置
      formConfig: [],
      // 查询栏 - 表单数据
      formData: {},
      slotList: [{ slotName: "searchBtn" }],
      // 查询条件：
      formData: { diagnosisItemSymbolNoList: [] },
      FishVarietyList: [],        // 下拉数据-品种
      FeaturesTypeManageList: [], // 诊断项名称-下拉数据
      BOList: [{}],               // 诊断项名称+选择诊断项症状编号的数据
      selectNoList: [],           // 选择诊断项症状-下拉列表
      resultList: [],             // 查询结果
      activeName: "1",            // 手风琴效果，每次只能打开一个
      disabledList: []
    };
  },
  created () {
    this.qureyFishVariety();
  },
  mounted () { },
  watch: {
    // 关联品种的el-select,完成联动清除
    'formData.fishVarietyId' (newVal, oldVal) {
      if (this.isSelectVisible) {
        // 清空 
        this.BOList.forEach((item, index) => {
          item.diagnosisItemId = '' // 诊断项名称
          item.diagnosisItemSymbolNoList = []  // 诊断项症状
          this.selectNoList = [] // 诊断项症状下拉列表
          // item[index]=[]
        })
        this.formData.diagnosisItemSymbolNoList = [] //清空已选特征名称
      }
      // if (!newVal) {
      //     this.BOList.forEach((item,index)=>{
      //       item.diagnosisItemId='' // 诊断项名称
      //       item.diagnosisItemSymbolNoList=[]  // 诊断项症状
      //       this.selectNoList=[] // 诊断项症状下拉列表
      //       item[index]=[]
      //     })
      //     this.formData.diagnosisItemSymbolNoList=[] //清空已选特征名称
      // }
    },
  },
  methods: {
    // 诊断项症状：鼠标移入显示提示框
    showAddress (diagnosisItemSymbolDesc) {
      this.diagnosisItemSymbolDesc = diagnosisItemSymbolDesc
      this.isshow = true
    },
    // 诊断项症状：鼠标移出隐藏提示框
    closeShowAddress () {
      this.isshow = false
    },
    // 关联品种
    qureyFishVariety () {
      FishVarietyApi.queryByCondition().then((res) => {
        this.FishVarietyList = res.data;
      });
    },
    // 关联品种 诊断项名称下拉框 出现true/隐藏false
    handleSelectVisibleChange (v) {
      this.isSelectVisible = v
      console.log('关联品种下拉触发', v);
      console.log('this.isSelectVisible', this.isSelectVisible);
    },
    // 诊断项名称
    qureyFeaturesTypeManage (e) {
      FeaturesTypeManageApi.queryByCondition({ fishVarietyId: e }).then((res) => {
        this.FeaturesTypeManageList = res.data;
      }
      );
    },
    // 诊断项症状
    qureyDiseaseFeatures (e, index) {
      this.BOList[index].diagnosisItemSymbolNoList = []  // 清空诊断项症状
      this.disabledList = []
      DiseaseFeaturesManageApi.queryByCondition({ diagnosisItemId: e }).then((res) => {
        this.$set(this.BOList[index], "diseaseFeaturesList", res.data);
      }
      );
      this.BOList.forEach(item => {
        this.disabledList.push(item.diagnosisItemId)
      })
    },
    // 选择诊断项症状
    updateSelected (e) {
      this.formData.diagnosisItemSymbolNoList = [];
      this.selectNoList = [];
      this.BOList.forEach((item) => {
        if (item.diagnosisItemSymbolNoList.length) {
          this.formData.diagnosisItemSymbolNoList.push(...item.diagnosisItemSymbolNoList); // 更新需要传给后端的诊断项症状编号
          // 处理《中间图片展示的相关数据》
          this.formData.diagnosisItemSymbolNoList.forEach((diagnosisItem) => {
            item.diseaseFeaturesList.find((featuresItem) => {
              if (featuresItem.diagnosisItemSymbolNo === diagnosisItem) {
                this.selectNoList.push(featuresItem);
              }
            });
          });
        }
      });
    },
    // 下拉框出现/隐藏时触发 - 图片下载
    updateImg (e) {
      if (!e) {
        // 当下拉框收起时
        this.selectNoList.forEach((item) => {
          if (item.fileIdList && item.fileIdList.length) {
            item.fileIdList.split(',').forEach((fileId) => {
              this.$set(item, "src", []);
              newDocumentApi.fileDownload({ fileId: fileId }).then((res) => {
                blobToBase64(res).then((imageUrl) => {
                  item.src.push(imageUrl)
                });
              });
            })
          }
        });
      }
    },
    // 查询按钮
    judgeDisease () {
      DiseaseManageApi.judgeDisease(this.formData).then((res) => {
        console.log("诊断结果", res);
        const data = res.data;
        if (data) {
          this.resultList = data;
        }
      });
    },
    // 点击添加按按钮
    addRow () {
      let list = {};
      if (!this.formData.fishVarietyId) {
        this.$message.info('请先选择鱼种！')
        return
      }
      if (this.BOList.length < this.FeaturesTypeManageList.length) {
        this.BOList.push(list);
      }
    },
    // // 删除改行
    // deleteRow(index) {
    //   this.BOList.splice(index, 1);
    // },
    // 删除改行
    deleteRow (index, item) {
      this.disabledList = []
      this.BOList.splice(index, 1);
      this.BOList.forEach(item => {
        this.disabledList.push(item.diagnosisItemId)
      })
      let id = item.diagnosisItemId
      let symbolList = item.diagnosisItemSymbolNoList
      if (id) {
        this.selectNoList = this.selectNoList.filter((imgData) => {
          return imgData.diagnosisItemId != id
        })
      }
      if (symbolList && symbolList.length) {
        symbolList.forEach(No => {
          this.formData.diagnosisItemSymbolNoList = this.formData.diagnosisItemSymbolNoList.filter(symbol => {
            console.log(symbol)
            return symbol != No
          })
        })
      }
    },
  },
};
</script>

<style lang="scss" scoped>
// dialog框header背景色
::v-deep .el-dialog__header {
  background: #0480fb;
}

// 设置dialog框叉号颜色
::v-deep .el-dialog__close {
  color: #fff;
}

// dialog框标题颜色
::v-deep .el-dialog__title {
  color: #fff;
}
</style>

<style>
/* 图片上传 */
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

/* 选择诊断项症状-下拉列表 */
.dSymbolNoList {
  width: 100px;
}
</style>

<style lang="scss" scoped>
/* 查询表单 */
.select-box-container {
  height: 100%;
}

.select-box-item {
  margin-bottom: 10px;
}

/* 诊断项名称 */
.select-box-item-one {
  width: 300px;
  display: flex;
  margin-right: 20px;

  span {
    width: 100px;
    line-height: 30px;
  }
}

/* 诊断项名称和选择诊断项症状编号 */
.name-number {
  /* height: 200px; */
  display: flex;

  /* 选择诊断项症状编号框 */
  .select-box-item-three {
    display: flex;

    /* background: #000; */
    span {
      width: 130px;
      line-height: 30px;
    }
  }
}

/* 选择诊断项症状 */
.select-box-item-three {

  /* 选择诊断项症状 - 下拉框每项 */
  ::v-deep .el-select__tags .el-select__tags-text {
    /* background: #000; */
    display: inline-block;
    max-width: 100px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
  ::v-deep .el-select--small{
    width: 220px;
  }
}

/* 诊断项症状下拉列表的提示信息 */
.dsdTooltip {
  position: fixed;
  top: 0vh;
  left: 50%;
  transform: translate(-50%, 0);
  white-space: pre-wrap;
  padding: 0px 20px;
  border-radius: 30px;
  font-size: 20px;
  background: rgb(255, 255, 255);
  color: rgb(64, 158, 255);
  border: 2px solid rgb(220, 230, 190);
}

/* 按钮区域 */
.select-box-item-son {
  display: flex;

  /* 按钮 */
  .line-click {
    position: absolute;
  }
}

/* 特征图片+预防/治疗方法 */
.sectionOutermost {
  height: 100%;
  overflow: auto;

  /* 特征图片 */
  .section-center {
    width: 100%;
    // height: 484px;
    padding: 20px;
    border-radius: 5px 5px 5px 5px;

    .selected-box {
      color: black;
      font-size: 16px;
      height: 44px;

      >span:nth-child(1) {
        color: #409eff;
        font-weight: 600;
      }

      .selected-btn {
        width: 30px;
        height: 30px;
        padding: 4px;
        margin: 0 4px;
        border: 1px solid #00305f;
        background: #3580c9;
        border-radius: 5px 5px 5px 5px;
        color: #fff;
      }
    }

    .picture-box {
      display: flex;
      overflow: auto;

      .diagnosis {
        width: 400px;
        height: 260px;
        background: #edf6ff;
        box-shadow: 0px 1px 3px 1px rgba(4, 128, 251, 0.35);
        margin-right: 20px;

        .diagnosis-header {
          width: 400px;
          height: 32px;
          padding: 0 10px;
          background: #0480fb;
          color: #fff;
          font-size: 16px;
          display: flex;
          align-items: center;

          &-title {
            font-weight: bold;
          }

          &-desc {
            text-overflow: ellipsis;
            overflow: hidden;
            word-break: break-all;
            white-space: nowrap;
          }
        }

        /*  */
        .diagnosis-body {
          padding: 10px;
          width: 100%;
          height: calc(100% - 32px);
          display: flex;
          justify-content: center;
          align-items: center;
          position: relative;

          &-empty {
            font-size: 20px;
          }

          &-img {
            width: 482px;
            height: 350px;
            object-fit: cover;
          }

          .numFont {
            position: absolute;
            bottom: 10px;
            right: 10px;
            padding: 4px 10px;
            background: rgba(141, 170, 232, 0.5);
          }
        }
      }
    }
  }

  /* 预防/治疗方法 */
  .section-bottom {
    margin: 16px 0;
    width: 100%;
    padding: 20px;
    border-radius: 5px 5px 5px 5px;

    .result-item {
      width: 100%;
      height: 100%;
      margin: 10px;
      padding: 10px 10px 0;
      background: #ebf5ff;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      font-size: 16px;

      &-row {
        height: 100%;
        margin-bottom: 20px;
        display: flex;

        &-title {
          display: block;
          height: 100%;
          padding: 0 10px;
          line-height: 32px;
          background: #0480fb;
          border-radius: 5px;
          color: #fff;
          margin-right: 20px;
        }

        &-content {
          flex: 1;
          color: #2b2b2b;
          display: block;
          line-height: 32px;
        }
      }
    }
  }
}

/* 添加和减少按钮 */
.line-click {
  background: transparent;
  margin-bottom: 0;
  font-size: 32px;

  .el-icon-circle-plus-outline {
    position: absolute;
    left: 0;
    top: 0;
    color: rgb(64, 158, 255);
  }

  .el-icon-remove-outline {
    position: absolute;
    left: 0;
    top: 0;
    color: rgb(64, 158, 255);
  }
}

.epidemic-database {
  color: rgb(64, 158, 255);
  font-size: 18px;
  font-weight: bolder;

  .epidemic-database-star {
    font-size: 25px;
    font-weight: bolder;
  }
}
</style>
